package com.mydemo.game.prepareStage;

import com.mydemo.entity.Car;
import com.mydemo.entity.Driver;
import com.mydemo.entity.Save;
import com.mydemo.print.Print;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class DriverLeftCar {
    static Scanner scanner = new Scanner(System.in);
    public static final Save driverLeftCar(Save save){
        System.out.println("当前有车辆的司机");
        List<Driver> driverList = new ArrayList<>();
        if (save.getDriverList() != null){
            driverList = save.getDriverList();
        }else {
            System.out.println("当前没有任何司机指派的有车辆");
            return save;
        }
        List<Driver> hasCar = new ArrayList<>();
        for (Driver d :
                driverList) {
            if (d.getCar() != 0)hasCar.add(d);
        }
        System.out.println("当前指派过汽车的司机");
        for (Driver d :
                hasCar) {
            Print.printDriver(d);
        }
        System.out.println("按下id进行选择，按0结束");
        int choose = scanner.nextInt();
        Driver selectDriver = new Driver();
        if (choose == 0){
            return save;
        }else {
            selectDriver = selectDriver(driverList,choose);
            driverList.remove(selectDriver);
            List<Car> carList = save.getCarList();
            for (Car c :
                    carList) {
                if (c.getId() == selectDriver.getCar()){
//                    carList.remove(c);
                    c.setDrive(false);
//                    carList.add(c);
                }
            }
            selectDriver.setCar(0);
            driverList.add(selectDriver);
            save.setCarList(carList);
            save.setDriverList(driverList);
        }
        return save;
    }
    
    private static final Driver selectDriver(List<Driver> driverList,int id){
        for (Driver driver :
                driverList) {
            if (driver.getId() == id)return driver;
        }
        return null;
    }
}
